package org.visionlibrary.image.geomtric.lines.histogram;

import java.util.List;

import org.visionlibrary.image.geomtric.lines.histogram.model.AbstractLinesHistogram;
import org.visionlibrary.image.geomtric.model.Vector2d;
import org.visionlibrary.image.model.Histogram;


/**
 * Klasa realizujaca generowanie histogramu dla zbioru odcinkow na podstawie ich
 * kata nachylenia.
 */
public class LineSlope extends AbstractLinesHistogram {
	//histogram posiadajacy 180 lub 360 binow jest malo dokladny
	//aby zwiekszyc dokladnosc katy mapowane sa na pewien przedzial liczb
	//calkowitych
	protected int histogramSize = 10000;

	public LineSlope(int histogramSize) {
		super();
		this.histogramSize = histogramSize;
	}

	@Override
	public Histogram process(List<Vector2d> lines) {
		if (null == lines)
			throw new NullPointerException("Lines list is null.");
		
		Histogram histogram = new Histogram(histogramSize + 1);
		for (Vector2d line : lines) {
			double angle = line.angle();
			int index = (int) t(angle, histogramSize);
			histogram.addToBin(index, line.length());
		}

		return histogram;
	}
	
	private double t(double angle, int precision) {
		double maxtheta = Math.PI;
		double thetadiff = maxtheta - angle;
		return precision - (precision * thetadiff / maxtheta);
	}
}
